package com.kinptm.masterclock.ui.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.ViewFlipper;

import com.actionbarsherlock.app.SherlockListFragment;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import com.kinptm.masterclock.Intents;
import com.kinptm.masterclock.R;
import com.kinptm.masterclock.adapter.HolderViewFiller;
import com.kinptm.masterclock.adapter.item.AlarmClockItemBuilder;
import com.kinptm.masterclock.bean.AlarmClock;
import com.kinptm.masterclock.db.DbHelper;
import com.kinptm.masterclock.utils.ViewUtil;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by pc on 13-11-7.
 */
public class AlarmClockFragment extends SherlockListFragment implements View.OnClickListener {
    private static final int REQUEST_CODE_ADD = 1;
    private static final int REQUEST_CODE_CHECK = 2;
    private HolderViewFiller<AlarmClock> mHolderViewFiller;
    private List<AlarmClock> mAlarmClocks;
    private ViewFlipper mSwapViewFlipper;

    public static AlarmClockFragment newInstance() {
        AlarmClockFragment fragment = new AlarmClockFragment();
//        Bundle bundle = new Bundle();
//        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_alarm_clock, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mSwapViewFlipper = ViewUtil.findView(view, R.id.swap_layout_vf);
        ViewUtil.findViewAndClick(view, R.id.alarm_clock_add_btn, this);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setHasOptionsMenu(true);
        try {
            mAlarmClocks = DbHelper.getInstance().getAlarmClockDao().queryBuilder().orderBy("id", false).query();
        } catch (SQLException e) {
            e.printStackTrace();
            mAlarmClocks = new ArrayList<AlarmClock>();
        }
        mHolderViewFiller = new HolderViewFiller<AlarmClock>(getLayoutInflater(savedInstanceState), new AlarmClockItemBuilder());
        mHolderViewFiller.update(getListView(), mAlarmClocks);
        checkSwapViewFlipper();
    }

    private void checkSwapViewFlipper() {
        if (mAlarmClocks.isEmpty()) {
            mSwapViewFlipper.setDisplayedChild(1);
        } else {
            mSwapViewFlipper.setDisplayedChild(0);
        }
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.fragment_alarm_clock, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_alarm_clock_add:
                Intents.launchAddAlarmClock(this, REQUEST_CODE_ADD);
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.add("删除");
    }

    @Override
    public boolean onContextItemSelected(android.view.MenuItem item) {
        int position = ((AdapterView.AdapterContextMenuInfo) item.getMenuInfo()).position;
        AlarmClock clock = mAlarmClocks.get(position);
        mAlarmClocks.remove(position);
        mHolderViewFiller.exportAdapter(getListView()).notifyDataSetChanged();
        DbHelper.getInstance().getAlarmClockDao().delete(clock);
        return super.onContextItemSelected(item);
    }

    @Override
    public void onResume() {
        super.onResume();
        registerForContextMenu(getListView());
    }

    @Override
    public void onPause() {
        super.onPause();
        unregisterForContextMenu(getListView());
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        Intents.launchAddAlarmClock(this, REQUEST_CODE_CHECK, (AlarmClock) l.getItemAtPosition(position));
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK && data != null && data.getExtras() != null) {
            AlarmClock alarmClock = (AlarmClock) data.getExtras().getSerializable(Intents.ALARM.EXTRA_ADD_ALARM_CLOCK);
            switch (requestCode) {
                case REQUEST_CODE_ADD:
                    mAlarmClocks.add(0, alarmClock);
                    mHolderViewFiller.exportAdapter(getListView()).notifyDataSetChanged();
                    break;
                case REQUEST_CODE_CHECK:
                    int index = mAlarmClocks.indexOf(alarmClock);
                    mAlarmClocks.remove(index);
                    mAlarmClocks.add(index, alarmClock);

//                    AlarmClock clock = mAlarmClocks.get(index);
//                    clock.setEnable(alarmClock.isEnable());
//                    clock.setAlarmMillis(alarmClock.getAlarmMillis());
//                    clock.setRemark(alarmClock.getRemark());
//                    clock.setRepeat(alarmClock.getRepeat());
//                    clock.setSound(alarmClock.isSound());
//                    clock.setSoundUri(alarmClock.getSoundUri());
//                    clock.setVibrate(alarmClock.isVibrate());
                    mHolderViewFiller.exportAdapter(getListView()).notifyDataSetChanged();
                    break;
            }
            checkSwapViewFlipper();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case  R.id.alarm_clock_add_btn:
                Intents.launchAddAlarmClock(this, REQUEST_CODE_ADD);
                break;
        }
    }
}